﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PM.Entity;
using PM.Services;
using System.Collections.Generic;

namespace PM.Site.Admin
{
    public partial class PortfolioOverview : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null || Session["UserTypeId"] == null)
            {
                Response.Redirect("Login.aspx?RequestUrl=" + Request.Url.OriginalString);
            }
            else if (Session["UserTypeId"] != null)
            {
                if (Int32.Parse(Session["UserTypeId"].ToString()) == 1)
                {
                    this.Loading();
                }
                else
                {
                    Response.Redirect("Default.aspx?RequestUrl=" + Request.Url.OriginalString);
                }

            }
            else
            {
                Response.Redirect("Default.aspx?RequestUrl=" + Request.Url.OriginalString);
            }

        }

        private void Loading()
        {
            if (!Page.IsPostBack)
            {
                PortfolioCollection ports = PortfolioServices.GetPortfolioCollection();
                dlPortfolios.DataSource = ports;
                dlPortfolios.DataBind();

                ddlClientIds.DataSource = ClientServices.GetClientID_Name_Collection();
                ddlClientIds.DataTextField = "Other_Names";
                ddlClientIds.DataValueField = "ClientId";
                ddlClientIds.AppendDataBoundItems = true;
                ddlClientIds.DataBind();

                ddlSecurities.DataSource = SecurityServices.Get_SecurityID_Name_Collection();
                ddlSecurities.DataTextField = "Name";
                ddlSecurities.DataValueField = "SecurityId";
                ddlSecurities.AppendDataBoundItems = true;
                ddlSecurities.DataBind();

            }
        }

        protected void ddlClientIds_SelectedIndexChanged(object sender, EventArgs e)
        {
            populate();
        }

        protected void ddlSecurities_SelectedIndexChanged(object sender, EventArgs e)
        {
            populate();
        }

        private void populate()
        {
            dlPortfolios.DataSource = PortfolioServices.Get_Portfolio_Collection_By_ClientId_SecurityId(ddlClientIds.SelectedValue, ddlSecurities.SelectedValue);
            dlPortfolios.DataBind();

        }
    }
}
